﻿using SuperX.Common.Collection;
using SuperX.Common.Helper;
using SuperX.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using SuperX.Helper.Validation;
using Newtonsoft.Json;

namespace SuperX.DataService
{
    public class DriverConfig : BasicObjCollection<IBasicObject>
    {
        private string _id = GenerateUid.GenerateSnowFlake().ToString();

        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool Enabled { get; set; } = true;

        /// <summary>
        /// 预配置驱动名
        /// </summary>
        [Empty]
        public string PreDriverName { get; set; }

        [Empty]
        public string AssemblyName { get; set; }

        public string ClassFullName { get; set; }

        /// <summary>
        /// 分组名
        /// </summary>
        public string GroupName { get; set; }

        /// <summary>
        /// 模板ID
        /// </summary>
        public string TemplateId { get; set; }

        /// <summary>
        /// 是否与模板同步
        /// </summary>
        public bool TemplateSync { get; set; }

        /// <summary>
        /// 厂商名
        /// </summary>
        public string Manufacture { get; set; }

        /// <summary>
        /// 设备编码
        /// </summary>
        public string DriverNO { get; set; }

        /// <summary>
        /// 节点名称
        /// </summary>
        public string NodeKey
        {
            get => _nodeKey;
            set
            {
                _nodeKey = value;
            }
        }

        private string _nodeKey = "Server";

        public override string FullName
        {
            get
            {
                if (Parent != null && Parent.FullName != null && !string.IsNullOrEmpty(NodeKey))
                {
                    return $"{Parent.FullName}.{NodeKey}.{Name}";
                }
                return base.FullName;
            }
        }

        private ObjectCollection<DriverConfig, GroupConfig> _groupConfigs;

        public ObjectCollection<DriverConfig, GroupConfig> Groups
        {
            get { return _groupConfigs ?? (_groupConfigs = new ObjectCollection<DriverConfig, GroupConfig>(this)); }
            set
            {
                _groupConfigs = value;
                _groupConfigs.SetParent(this);
            }
        }

        public Dictionary<string, ArgumentModel> Argument
        {
            get => _argument ?? (_argument = new Dictionary<string, ArgumentModel>());
            set
            {
                _argument = value;
            }
        }

        /// <summary>
        /// 0:为正常输入地址变量模式 1：从树和表格带出
        /// </summary>
        public int AddressExtend { get; set; }

        public override IBasicObject this[string name] => Groups[name];

        public override List<string> GetKeys()
        {
            return Groups.Keys.ToList();
        }

        public override List<IBasicObject> GetValues()
        {
            var group = Groups.Values.OfType<IBasicObject>().ToList();
            return group;
        }

        public override bool IsUniqueName(string name, IObjCollection parent)
        {
            if (parent != null)
            {
                return parent.IsUniqueName(name, null);
            }

            return Groups[name] == null;
        }

        private Dictionary<string, ArgumentModel> _argument;
    }

    public class GroupConfig : BasicObjCollection<IBasicObject>
    {
        public int UpdateRate { get; set; }

        public int DeadBand { get; set; }

        [SuperX.Helper.Validation.Empty]
        public bool IsActive { get; set; }

        public TagType GroupType { get; set; }

        private ObjectCollection<GroupConfig, TagConfig> _tagGroups;

        public ObjectCollection<GroupConfig, TagConfig> TagGroups
        {
            get => _tagGroups ?? (_tagGroups = new ObjectCollection<GroupConfig, TagConfig>(this));
            set
            {
                _tagGroups = value;
                _tagGroups.SetParent(this);
            }
        }

        public override IBasicObject this[string name] => TagGroups[name];

        public override List<string> GetKeys()
        {
            return TagGroups.Keys.ToList();
        }

        public override List<IBasicObject> GetValues()
        {
            var group = TagGroups.Values.OfType<IBasicObject>().ToList();
            return group;
        }

        public override bool IsUniqueName(string name, IObjCollection parent)
        {
            if (parent != null)
            {
                return parent.IsUniqueName(name, null);
            }

            return TagGroups[name] == null;
        }
    }
}